// Given a string, find the length of the longest substring without repeating characters.
// Examples:
// Given "abcabcbb", the answer is "abc", which the length is 3.
// Given "bbbbb", the answer is "b", with the length of 1.
// Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

// 回溯

/**
 * 字符串中不重复字符串的长度
 * @param  {[sring]} s [原字符串]
 * @return {[number]}  [字符串长度]
 */
var lengthOfLongestSubstring = function(s) {
  var left = 0;
  var pre = [];
  var length = 0;
  for (var i = 0; i < s.length; i++) {
    if (pre[s.charAt(i)] >= left) {
      left = pre[s.charAt(i)] + 1;
    }
    pre[s.charAt(i)] = i;
    if (length < i - left + 1) {
      length = i - left + 1;
    }
  }
  return length;
};

console.log(lengthOfLongestSubstring('wwddww'));
